//
//  GGVistorView.swift
//  SwiftWeico
//
//  Created by denjunjie1 on 15/12/15.
//  Copyright © 2015年 Gel. All rights reserved.
//

import UIKit

protocol GGVistorViewDelegate : NSObjectProtocol {

    func vistorViewLogin()

}



class GGVistorView: UIView {

   
    weak var delegate : GGVistorViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    
    func loginDidClick() {
    
        delegate?.vistorViewLogin()
    }
    
    
    func rotationAnimation() {
    
        let rotatin = CABasicAnimation(keyPath: "transform.rotation")
        
        rotatin.toValue = 2 * M_PI
        rotatin.duration = 20
        rotatin.repeatCount = MAXFLOAT
        
        rotatin.removedOnCompletion = false
        
        iconView.layer.addAnimation(rotatin, forKey: "rotation")
    
    }
    
    func pauseAnimation() {
    
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        iconView.layer.speed = 0
        
        iconView.layer.timeOffset = pauseTime
    }
    
    
    func resumeAnimation() {
    
        let pauseTime = iconView.layer.timeOffset
        
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSicnPause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
    
        iconView.layer.beginTime = timeSicnPause
    
    }
    
    
    func setupVistorView(imgName : String , msg : String){
    
        iconView.image = UIImage(named: imgName)
        
        msgLable.text = msg
        
        msgLable.sizeToFit()
        
        homeView.hidden = true
        
        coverView.hidden = true
    }

    private func prepareUI(){
    
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(msgLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
   
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        msgLable.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
    
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -50))
        
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        self.addConstraint(NSLayoutConstraint(item: msgLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: msgLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: msgLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 220))
        
        
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: msgLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: msgLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: msgLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: msgLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        
        
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
    
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn  , attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        

    
    
    
    }
    
    
    
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var homeView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var msgLable : UILabel = {
    
        let label = UILabel()
        
        label.text = "关注一些人,看看这里有什么惊喜!"
        
        label.sizeToFit()
        
        label.textColor = UIColor.lightGrayColor()
        
        label.numberOfLines = 0
        
        return label
    
    }()
    
    private lazy var registerBtn : UIButton = {
    
        let registerBtn = UIButton()
        
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        registerBtn.setTitle("Register", forState: UIControlState.Normal)
        
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        registerBtn.sizeToFit()
        
        return registerBtn
    
    }()
    
    
    private lazy var loginBtn : UIButton = {
    
        let loginBtn = UIButton()
        
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        loginBtn.setTitle("Login", forState: UIControlState.Normal)
        
        loginBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        loginBtn.sizeToFit()
        
        loginBtn.addTarget(self, action: "loginDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return loginBtn
    }()
    
    
    private lazy var coverView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
